package main

import (
	"go-ddd-ecommerce/internal/application/services"
	"go-ddd-ecommerce/internal/domain/events"
	domainServices "go-ddd-ecommerce/internal/domain/services"
	"go-ddd-ecommerce/internal/infrastructure/persistence/mysql"
	simpleEventBus "go-ddd-ecommerce/internal/infrastructure/simple_event_bus"
	"go-ddd-ecommerce/internal/interfaces/handlers"
	"go-ddd-ecommerce/internal/interfaces/routers"
)

func main() {
	// 初始化数据库
	db := mysql.Connect()
	if db == nil {
		return
	}
	// 初始化事件总线
	eventBus := simpleEventBus.NewSimpleEventBus()
	// 初始化仓储（基础设施层）
	userRepo := mysql.NewUserRepository(db)
	productRepo := mysql.NewProductRepository(db)
	orderRepo := mysql.NewOrderRepository(db)
	purchassService := domainServices.NewPurchaseService()
	eventPublisher := events.NewDomainEventPublisher(eventBus)

	// 初始化应用服务
	orderAppService := services.NewOrderAppService(userRepo, productRepo, orderRepo, purchassService, eventPublisher, db)
	userAppService := services.NewUserAppService(userRepo, eventPublisher)
	productAppService := services.NewProductAppService(productRepo, eventPublisher)

	// 初始化处理器
	orderHandler := handlers.NewOrderController(orderAppService)
	userHandler := handlers.NewUserController(userAppService)
	productHandler := handlers.NewProductController(productAppService)

	// 设置路由
	r := routers.NewRouter(userHandler, productHandler, orderHandler)
	router := r.SetupRoutes()
	// 启动服务器

	router.Run(":8080")

}
